﻿using UnityEngine;
using System.Collections;
using System;

namespace WuZiLianZhu{
	public enum SNSPlatform{
        LocalTest = -1,
        NoPlatform = 0,
        Facebook = 1,
        SinaWeibo = 2,
        Wechat = 3,
        MobilePhoneNumber = 4,
        AndroidChannel = 5
    }

	public enum AvatarType{
	    Local = 0,
	    HTTP
	}

    public class UserInfo
    {
        public SNSPlatform Platform;
        public string BindingId;
        public string Id; // 服务器存的是INT32，客户端用的是String
        public string Name;
        public AvatarType AvatarType;
        public string AvatarData;
        public int LastStageId;
        public int StarCount;
        public int RainbowCount;
        public int WingCount;
        public long LastLoginTime;
        public int CrtSkin;

        // 构造函数
        public UserInfo()
        {
            Platform = SNSPlatform.NoPlatform;
            BindingId = "DefaultBingdingId";
            Id = "DefaultId";
            Name = "";
            AvatarType = AvatarType.Local;
            AvatarData = "LocalAvatarGirl";
            LastStageId = 1;
            StarCount = 0;
            RainbowCount = 0;
            WingCount = 0;
            LastLoginTime = 0;
            CrtSkin = 0;
        }

        // 输出成Json String
        public string ToJson(){
            Hashtable ht = new Hashtable();
            ht["platform"] = (int)Platform;
            ht["bindingId"] = BindingId;
            ht["id"] = Id;
            ht["name"] = Name;

            ht["avatarType"] = (int)AvatarType;
            ht["avatarData"] = (string)AvatarData;

            ht["lastStageID"] = LastStageId;
            ht["star"] = StarCount;
            ht["rainbow"] = RainbowCount;
            ht["wing"] = WingCount;

            ht["lastLoginTime"] = LastLoginTime.ToString();

            ht["CrtSkin"] = CrtSkin;
                
            string json = MiniJsonExtensions.toJson(ht);

            return json;
        }

        // 由Json String转成具体的值
        public void SetByJson(string json){
            try{
                Hashtable ht = MiniJsonExtensions.hashtableFromJson(json);

                Platform = (SNSPlatform)Convert.ToInt32(ht["platform"]);
                Id = Convert.ToString (ht["id"]);
                BindingId = Convert.ToString (ht["bindingId"]);
                Name = (string) ht["name"];

                AvatarType = (AvatarType)System.Convert.ToInt32(ht["avatarType"]);
                AvatarData = (string)ht["avatarData"];

                LastStageId = System.Convert.ToInt32(ht["lastStageID"]);
                StarCount = System.Convert.ToInt32(ht["star"]);
                RainbowCount = System.Convert.ToInt32(ht["rainbow"]);
                WingCount = System.Convert.ToInt32(ht["wing"]);

                if (ht.ContainsKey("lastLoginTime")){
                    LastLoginTime = System.Convert.ToInt64(ht["lastLoginTime"]);
                }

                if (ht.ContainsKey("CrtSkin")){
                    CrtSkin = System.Convert.ToInt32(ht["CrtSkin"]);
                }
            }catch(System.Exception e){
                Debug.LogError("SetUserInfoBy Error: " + e.ToString());
            }
        }
    }
}
